package UIUtils {
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class uif_utilR {
		
		public static const OR_HORI: uint = 0;
		public static const OR_VERT: uint = 1;
		
		public static function getrect(cp: Object): Rectangle {
			var rect: Rectangle;
			if (cp is uif_component) rect = cp.getInner();
			else rect = cp.clone();
			return rect;
		};
		
		public static function hdiv(cp: Object, space: Number = 0, num: uint=1, ncount: uint=1, acount: uint=2): Rectangle {
			return div(cp, OR_HORI, space, num, ncount, acount);
		}
		
		public static function vdiv(cp: Object, space: Number = 0, num: uint=1, ncount: uint=1, acount: uint=2): Rectangle {
			return div(cp, OR_VERT, space, num, ncount, acount);
		}
		
		public static function div(cp: Object, orientation: uint=OR_HORI, space: Number = 0, num: uint=1, ncount: uint=1, acount: uint=2): Rectangle {
			var rect: Rectangle = getrect(cp);
			if (OR_HORI) {
				rect.width = rect.width * ncount/acount;
				rect.x += rect.width * (num - 1) + space / 2;
				rect.width -= space;
			} else {
				rect.height = rect.height * ncount/acount;
				rect.y += rect.height * (num - 1) + space / 2;
				rect.height -= space;
			}
			return rect;
		}
		
		public static function waitNoOut(component: EventDispatcher, proc: Function, waitmls: uint=500): void {
			function removeEvents(): void {
				component.removeEventListener(MouseEvent.ROLL_OUT, doOut);
				component.removeEventListener(MouseEvent.MOUSE_DOWN, doOut);
			}
			
			var timer: uif_Timer = uif_Timer.one(waitmls, function(e: Event): void {
				removeEvents();
				proc();
			});
			function doOut(e: MouseEvent): void {
				removeEvents();
				timer.stop();
			}
			component.addEventListener(MouseEvent.ROLL_OUT, doOut);
			component.addEventListener(MouseEvent.MOUSE_DOWN, doOut);
		}
	}
}